package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.MessageConstant;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文章详情相关接口
 */
@RestController
@RequestMapping("/content")
@Api(tags = "文章详情相关接口")
public class ContentController {

    @Autowired
    private BlogService blogService;

    /**
     * 通过Uid获取博客内容
     * @param uid
     * @return
     */
    @GetMapping("/getBlogByUid/{uid}")
    @ApiOperation("通过Uid获取博客内容")
    public String getBlogByUid(@PathVariable String uid){
        if ( uid==null||"".equals( uid)){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }
        Blog blog = blogService.getBlogByUid(uid);
        if (blog==null){
            return ResultUtil.result("401", MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200", blog);
    }

    /**
     * 通过Uid获取博客内容
     * @param uid
     * @return
     */
    @GetMapping("/getBlogPraiseCountByUid/{uid}")
    @ApiOperation("通过Uid获取文章点击数")
    public String getBlogPraiseCountByUid(@PathVariable String uid){

        Integer count = blogService.getBlogPraiseCountByUid(uid);
        if (count==null){
            return ResultUtil.result("401", MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200",count);
    }

    /**
     * 根据BlogUid获取同类型的博客
     * @param blogUid
     * @return
     */
    @GetMapping("/getSameBlogByBlogUid/{blogUid}")
    @ApiOperation("根据BlogUid获取同类型的博客")
    public String getSameBlogByBlogUid(@PathVariable String blogUid){

        List<Blog> blogList = blogService.getSameBlogByBlogUid(blogUid);
        if (blogList==null){
            return ResultUtil.result("401", MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200",blogList);
    }

    /**
     *
     * @param tagUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getSameBlogByTagUid/{tagUid}/{currentPage}/{pageSize}")
    @ApiOperation("根据标签uid获取相关博客")
    public String getSameBlogByTagUid(@PathVariable("tagUid")String tagUid ,@PathVariable("currentPage")Long currentPage,@PathVariable("pageSize")Long pageSize){
        if (currentPage<=0L||pageSize<=0L){
            currentPage=1L;
            pageSize=10L;
        }
        Page<Blog> blogList = blogService.getSameBlogByTagUid(tagUid, currentPage, pageSize);
        if (blogList==null){
            return ResultUtil.result("401", MessageConstant.TAG_NULL_FAIL);
        }
        return ResultUtil.result("200",blogList);
    }

    @GetMapping("/praiseBlogByUid/{uid}")
    @ApiOperation("通过uid给博客点赞")
    public String praiseBlogByUid(@PathVariable("uid")String uid){
        return blogService.praiseBlogByUid(uid);
    }


}
